﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL2;
using BLL;

public partial class Problems2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            bindProblem();
        }
    }

    void bindProblem()
    {
        if (Request["Id"] != null)
        {
            DAL2.SecuritySubject subject = Security2.getSubjectByID(Request["Id"]);
            lblsubject.Text = subject.Caption;
            dlproblems.DataSource = Security2.getSecurityProblemList(Request["Id"]);
            dlproblems.DataBind();
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
    }
    DAL2.SecurityEvaluate2DataContext db = new SecurityEvaluate2DataContext();
    protected void dlproblems_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Label ID = e.Item.FindControl("ID") as Label;
            ID.Text = e.Item.ItemIndex + 1 + "、";
            DAL2.SecurityProblem problem = e.Item.DataItem as DAL2.SecurityProblem;
            Label lblId = e.Item.FindControl("lblId") as Label;
            lblId.Text = problem.Id.ToString();
            Label lbltitle = e.Item.FindControl("lbltitle") as Label;
            lbltitle.Text = problem.Title;
            List<DAL2.SecurityOption> options = (from aa in db.SecurityOption where aa.PId == problem.Id orderby aa.No select aa).ToList();
            if (problem.IsMultiple.Value == 1)
            {
                CheckBoxList cblselect = e.Item.FindControl("cblselect") as CheckBoxList;
                foreach (DAL2.SecurityOption option in options)
                {
                    cblselect.Items.Add(new ListItem(option.No + "、" + option.OptionContent, option.Id.ToString()));
                }
            }
            else
            {
                RadioButtonList rblselect = e.Item.FindControl("rblselect") as RadioButtonList;
                foreach (DAL2.SecurityOption option in options)
                {
                    rblselect.Items.Add(new ListItem(option.No + "、" + option.OptionContent, option.Id.ToString()));
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //try
        //{
            List<DAL2.SecurityResult> resultList = new List<DAL2.SecurityResult>();
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            Guid Pid = Guid.Empty;
            foreach (DataListItem item in dlproblems.Items)
            {
                Label lblId = item.FindControl("lblId") as Label;
                decimal Score = 0;
                string result = "";
                Pid = new Guid(lblId.Text);
                DAL2.SecurityProblem problem = Security2.getProblemByID(lblId.Text);
                if (problem.IsMultiple.Value == 1)//多选 
                {
                    CheckBoxList cblselect = item.FindControl("cblselect") as CheckBoxList;

                    foreach (ListItem cblItem in cblselect.Items)
                    {
                        if (cblItem.Selected)
                        {
                            DAL2.SecurityOption option = Security2.getOptionByID(cblItem.Value);
                            if (option != null)
                            {
                                Score += option.Mark.Value;
                                result += option.No + ",";
                            }
                        }
                    }
                }
                else
                {
                    RadioButtonList rblselect = item.FindControl("rblselect") as RadioButtonList;

                    foreach (ListItem cblItem in rblselect.Items)
                    {
                        if (cblItem.Selected)
                        {
                            DAL2.SecurityOption option = Security2.getOptionByID(cblItem.Value);
                            if (option != null)
                            {
                                Score += option.Mark.Value;
                                result += option.No;
                                break;
                            }
                        }
                    }
                }

                DAL2.SecurityResult LastResult = new DAL2.SecurityResult();
                LastResult.Id = Guid.NewGuid();
                LastResult.Pid = Pid;
                LastResult.Result = result;
                LastResult.Score = Score;
                LastResult.SetDepartment = user.DepartmentID;
                LastResult.SetIp = Request.UserHostAddress;
                LastResult.SetTime = DateTime.Now;
                LastResult.Sid = new Guid(Request["Id"]);
                LastResult.SoldierID = UserGuid.GetUserID(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
                resultList.Add(LastResult);
            }

            SecurityEvaluate2DataContext db = new SecurityEvaluate2DataContext();
            db.SecurityResult.InsertAllOnSubmit(resultList);
            db.SubmitChanges();
            string strScr = "<script language=javascript>  window.close(); </script>";
            Page.RegisterClientScriptBlock("Close", strScr);
        //}
        //catch (Exception ex)
        //{
        //    Promt.Text = ex.Message;
        //}
    }
}
